import { _decorator, Component, Node, ccenum, input, Input, EventTouch, RigidBody, PhysicsSystem, PhysicsGroup, Vec3 } from 'cc';
import { CacheManager } from '../../manager/CacheManager';
const { ccclass, property } = _decorator;

@ccclass('PlayerAI')
export class PlayerAI extends Component {
    @property
    public hp: number = 100; // 血量

    @property(Node)
    public hpBar: Node | null = null; // 血量条

    @property
    public hpDecreaseRate: number = 1; // 血量递减速度

    @property
    public bulletTimeDuration: number = 5; // 子弹时间持续时间

    @property
    public bulletTimeCooldown: number = 10; // 子弹时间冷却时间

    @property
    public bulletTimeMultiplier: number = 2; // 子弹时间血量消耗倍数

    private isBulletTime: boolean = false;
    private bulletTimeTimer: number = 0;
    private cooldownTimer: number = 0;

    onLoad() {
        input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        this.schedule(this.decreaseHP, 1, -1, 0); // 每秒减少血量
    }

    start() {
        // 初始化物理系统
        PhysicsSystem.instance.enable = true;
        this.node.getComponent(RigidBody)!.linearDamping = 0.1; // 减少线性阻尼
    }

    update(dt: number) {
        if (this.isBulletTime) {
            this.bulletTimeTimer -= dt;
            if (this.bulletTimeTimer <= 0) {
                this.isBulletTime = false;
                this.unschedule(this.decreaseHP);
                this.schedule(this.decreaseHP, 1, -1, 0); // 恢复正常血量递减
            }
        } else {
            this.cooldownTimer += dt;
            if (this.cooldownTimer >= this.bulletTimeCooldown) {
                this.cooldownTimer = 0;
            }
        }

        this.updateHPBar();
    }

    onTouchStart(event: EventTouch) {
        if (this.cooldownTimer <= 0) {
            this.isBulletTime = true;
            this.bulletTimeTimer = this.bulletTimeDuration;
            this.unschedule(this.decreaseHP);
            this.schedule(this.decreaseHP, 1 / this.bulletTimeMultiplier, -1, 0); // 加快血量递减
        }
    }

    onTouchEnd(event: EventTouch) {
        if (this.isBulletTime) {
            this.isBulletTime = false;
            this.unschedule(this.decreaseHP);
            this.schedule(this.decreaseHP, 1, -1, 0); // 恢复正常血量递减
        }
    }

    decreaseHP() {
        this.hp -= this.hpDecreaseRate;
        if (this.hp <= 0) {
            this.gameOver();
        }
    }

    onCollisionEnter(other: RigidBody, self: RigidBody) {
        const groupName = PhysicsGroup[other.group];
        if (groupName === 'enemy') {
            this.hp += 10; // 击中敌人后增加血量
            other.node.destroy(); // 销毁敌人
        }
    }

    gameOver() {
        this.hp = 0;
        this.updateHPBar();
        this.node.active = false;
        // cc.director.loadScene('GameOver'); // 加载游戏结束场景
        CacheManager.game.gameOver();
    }

    updateHPBar() {
        if (this.hpBar) {
            this.hpBar.setPosition(new Vec3(this.node.position.x - 50, this.node.position.y + 30, 0));
            this.hpBar.setScale(new Vec3(this.hp / 100, 1, 1));
        }
    }
}
